// orderDetalis.js
// api
const $api = require('../../utils/request.js').Api;
//订单详情
Page({
  /**
   * 页面的初始数据
   */
  data: {
    openIdWy: wx.getStorageSync("openIdWy"),//小程序唯一ID
    isViewShow: true, //判断确认作品是否显示
    modalHidden: true,//电子稿预览弹窗
    imgUrl: "",//预览图片地址
    aa: [
      {
        nickName: "wang",
        reward: "2"
      }
    ],
    orderDetalisList: "",//订单详情
    orderNo: "", //订单编号
    rid: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //根据rid获取
    this.getOrderLog(options.rid);
    this.setData({
      orderNo: options.orderNo,
      rid: options.rid
    })
  },
  //跳转至首页
  handleGoToBack() {
    wx.redirectTo({
      url: '/pages/order/order?openId=' + this.data.openIdWy + '&orderId=',
    })
  },
  getOrderLog(rid) {
    let orderDetalisList = []
    let cgList = []
    $api.getOrderLog(rid).then((res) => {
      if(res.length == 0){
        wx.showToast({
          title: "暂无订单详情",
          icon: "success",
          duration: 3000
        })
      }
      for (var i = 0; i < res.length; i++) {
        var obj = {
          title: "",
          time: "",
        }
        if (res[i].content == "创建订单") {
          obj.title = res[i].content
          obj.time = res[i].createTime
          obj.num = 0
          orderDetalisList.push(obj)
        }
        if (res[i].content == "订单已接单") {
          obj.title = res[i].content
          obj.time = res[i].createTime
          obj.num = 1
          orderDetalisList.push(obj)
        }
        if (res[i].content == "校对通过") {
          obj.title = res[i].content
          obj.time = res[i].createTime
          obj.num = 3
          orderDetalisList.push(obj)
        }
        if ((res[i]).hasOwnProperty("fileNo")) {
          obj.title = (res[i].content).replace("已完成", "版")
          obj.fileNo = res[i].fileNo
          obj.time = (res[i].createTime).substring(0, 11)
          cgList.push(obj)
        }
        if (res[i].content == "已确认") {
          obj.title = res[i].content
          obj.time = res[i].createTime
          obj.num = 4
          orderDetalisList.push(obj)
        }
        if (res[i].content == "设计完成待制作") {
          obj.title = res[i].content
          obj.time = res[i].createTime
          obj.num = 5
          orderDetalisList.push(obj)
        }
        if (res[i].content == "制作完成") {
          obj.title = res[i].content
          obj.time = res[i].createTime
          obj.num = 6
          orderDetalisList.push(obj)
        }
        if (res[i].content == "订单完成") {
          obj.title = res[i].content
          obj.time = res[i].createTime
          obj.num = 7
          orderDetalisList.push(obj)
        }
      }
      //初稿判断
      if (cgList.length > 0) {
        var obj = {
          title: cgList,
          time: cgList[0].time,
          num: 2
        }
        orderDetalisList.push(obj);
        //初稿存在时，显示确认作品按钮
        this.setData({
          isViewShow: false
        })
      }
      //排序
      orderDetalisList.sort(function (a, b) {
        return a.num - b.num;
      })
      //存在客户已确认时，不显示确认作品按钮
      for (var i = 0; i < orderDetalisList.length; i++) {
        if (orderDetalisList[i].num == 2) {
          continue
        }
        if (orderDetalisList[i].title == "已确认") {
          //隐藏确认作品按钮
          this.setData({
            isViewShow: true
          })
          break
        }
      }
      this.setData({
        orderDetalisList: orderDetalisList
      })
    }).catch((error) => {
      console.log(error);
      if (error.code == 500) {
        wx.showToast({
          title: error.message,
          icon: 'error',
          duration: 2000//持续的时间
        })
      }
    });
  },
  submitForm() {
    console.info(this.data.orderNo);
    wx.showLoading({
      title: '加载中',
    })
    $api.confirmOrder(this.data.orderNo).then((res) => {
      wx.hideLoading();
      this.getOrderLog(this.data.rid);
    }).catch((error) => {
      wx.hideLoading()
      if (error.code == 500) {
        wx.showToast({
          title: error.message,
          icon: 'error',
          duration: 2000//持续的时间
        })
      }
    });
  },
  //请求电子稿链接
  onTapJump(params) {
    let dataset = params.currentTarget.dataset
    $api.getPreviewImage(dataset.fileno).then((res) => {
      this.setData({
        imgUrl: res,
        modalHidden: false
      })
    }).catch((error) => {
      if (error.code == 500) {
        wx.showToast({
          title: error.message,
          icon: 'error',
          duration: 2000//持续的时间
        })
      }
    });
  },
  /**
     * 显示弹窗
     */
  buttonTap: function () {
    this.setData({
      modalHidden: false
    })
  },

  /**
   * 点击取消
   */
  modalCandel: function () {
    // do something
    this.setData({
      modalHidden: true
    })
  },
  /**
   *  点击确认
   */
  modalConfirm: function () {
    // do something
    this.setData({
      modalHidden: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
